using System;
using Kiwi.Container.Scope;

namespace Kiwi.Container.Wcf
{
    public class WcfRequestContainerActivator : IWcfRequestContainerActivator
    {
        [ThreadStatic] private static IContainer _container;
        [ThreadStatic] private static bool _wcfActive;

        protected internal static bool WcfCallActive
        {
            get { return _wcfActive; }
            set
            {
                _wcfActive = value;
                if (!_wcfActive && (_container != null))
                {
                    _container.Dispose();
                    _container = null;
                }
            }
        }

        #region IWcfRequestContainerActivator Members

        public IContainer GetLocalContainer(IContainer parentContainer)
        {
            if (_wcfActive && (_container == null))
            {
                _container = parentContainer.CreateContainer(ContainerScope.WcfRequest);
            }
            return _container;
        }

        #endregion
    }
}